﻿from django.contrib import admin
from django.utils.html import format_html
from .models import Tag, Article


class TagAdmin(admin.ModelAdmin):
    list_display = ("name", "description", "is_active", "usage_count", "created_by", "created_at")
    list_filter = ("is_active", "created_at", "created_by")
    search_fields = ("name", "description")
    actions = ["batch_activate", "batch_deactivate", "batch_delete"]
    
    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        if not obj:  # 创建新对象时
            form.base_fields["created_by"].initial = request.user
        return form
    
    def save_model(self, request, obj, form, change):
        if not change:  # 创建新对象时
            obj.created_by = request.user
        super().save_model(request, obj, form, change)
    
    def batch_activate(self, request, queryset):
        queryset.update(is_active=True)
        self.message_user(request, f"成功启用 {queryset.count()} 个标签")
    batch_activate.short_description = "批量启用"
    
    def batch_deactivate(self, request, queryset):
        queryset.update(is_active=False)
        self.message_user(request, f"成功禁用 {queryset.count()} 个标签")
    batch_deactivate.short_description = "批量禁用"
    
    def batch_delete(self, request, queryset):
        queryset.delete()
        self.message_user(request, f"成功删除 {queryset.count()} 个标签")
    batch_delete.short_description = "批量删除"


class ArticleAdmin(admin.ModelAdmin):
    list_display = ("title", "status", "is_sticky", "publish_date", "view_count", "created_by", "created_at", "status_badge")
    list_filter = ("status", "is_sticky", "publish_date", "created_at", "created_by", "tags")
    search_fields = ("title", "summary", "content")
    prepopulated_fields = {"slug": ("title",)}
    filter_horizontal = ("tags",)
    actions = ["batch_publish", "batch_draft", "batch_offline", "batch_delete"]
    
    # 自定义状态标签显示
    def status_badge(self, obj):
        badges = {
            "draft": "<span class=\"badge badge-secondary\">草稿</span>",
            "published": "<span class=\"badge badge-success\">已发布</span>",
            "offline": "<span class=\"badge badge-danger\">已下架</span>"
        }
        return format_html(badges.get(obj.status, ""))
    status_badge.short_description = "状态"
    
    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        if not obj:  # 创建新对象时
            form.base_fields["created_by"].initial = request.user
        return form
    
    def save_model(self, request, obj, form, change):
        if not change:  # 创建新对象时
            obj.created_by = request.user
        super().save_model(request, obj, form, change)
    
    # 批量操作
    def batch_publish(self, request, queryset):
        from django.utils import timezone
        queryset.update(status="published", publish_date=timezone.now())
        self.message_user(request, f"成功发布 {queryset.count()} 篇文章")
    batch_publish.short_description = "批量发布"
    
    def batch_draft(self, request, queryset):
        queryset.update(status="draft")
        self.message_user(request, f"成功设为草稿 {queryset.count()} 篇文章")
    batch_draft.short_description = "批量设为草稿"
    
    def batch_offline(self, request, queryset):
        queryset.update(status="offline", publish_date=None)
        self.message_user(request, f"成功下架 {queryset.count()} 篇文章")
    batch_offline.short_description = "批量下架"
    
    def batch_delete(self, request, queryset):
        queryset.delete()
        self.message_user(request, f"成功删除 {queryset.count()} 篇文章")
    batch_delete.short_description = "批量删除"


admin.site.register(Tag, TagAdmin)
admin.site.register(Article, ArticleAdmin)
